<?php
namespace app\admin\model;


use think\Exception;
use think\facade\Db;
use think\facade\Request;
use think\Model;
//品牌页
class Brand  extends Model{
    protected $pk='brand_id';
    //brand 品牌列表sql语句   whereOr(用闭包方法)
    public static function brandList($where,$whereOr,$data){
        $brand=Brand::field(true)->order('sort_order asc')
            ->where($where)
            ->where(function ($query) use($whereOr){
                $query->whereOr($whereOr);
            })->paginate([
                'list_rows'=>5,
                'query'=>$data

            ]);

        return $brand;
    }

    //获取器 展示变中文输出
    public static function   getIfShowTextAttr($v,$data){
        if ($data['if_show']==1){
            return '<font color="#00CC99">是</font>';
        }else{
            return '<font color="#CE3E3E">否</font>';
        }
    }
    //获取器 推荐变中文输出
    public static function   getRecommendedTextAttr($v,$data){
        if ($data['recommended']==1){
            return '<font color="#00CC99">是</font>';
        }else{
            return '<font color="#CE3E3E">否</font>';
        }
    }
    //获取器 图片logo图片输出  需要写死路径
    public static function   getBrandLogoImgAttr($v,$data){
        if ($data['brand_logo']!=NULL){
            return "<img src='/images/{$data["brand_logo"]}' width='70' height='50'/>";
        }else{
            return  "<img src='/images/default.png' alt='' width='70' height='50' />";
        }
    }

    //brand 品牌添加
     public static function   brandAdd($post){
        try{
            $res=Brand::insert($post);
        }catch (Exception $e){
            $res=false;
        }
        return $res;

         }

     //brand  品牌修改
     public static function   brandEdit($param){
        if (Request::isPost()){
            try{
               $res=Brand::update($param);
            }catch (Exception $e){
                $res=false;
            }
            return $res;
        }else{
          $find=Brand::find($param['brand_id'])->toArray();
          return $find;
        }

         }

     //brand   品牌删除
     public static function   brandDel($get){
        try{
            $list=\app\admin\model\Goods::distinct(true)->column("brand");
              if (!in_array($get['id'],$list)){
                  $res=Brand::destroy($get['id']);
                  $msg='删除成功';
              }else{
                  $res=false;
                  $msg='删除失败(商品列表中含有此品牌商品)';
              }
        }catch (Exception $e){
            $res=false;
            $msg='删除失败';
        }

        return [$msg,$res];

         }
}